import React, { Component } from 'react';
import subject from '../images/home/subject.png'
import './fast.less'
import { FsatApi } from '../request/api'
import { connect } from 'react-redux'

class Fast extends Component {
  state = {
    records: [
      { id: 1, title: 'Linx面试题', },
      { id: 2, title: '企业面试题', },
      { id: 3, title: 'UI面试题', },
      { id: 4, title: 'JVM面试题', },
      { id: 5, title: 'H5面试题', },
      { id: 6, title: 'Web面试题', },
      { id: 7, title: 'java面试题', },
      { id: 8, title: 'Oracle面试题', },
      { id: 9, title: 'Redice面试题', },
      { id: 10, title: 'Vue面试题', },
      { id: 11, title: 'React面试题', },
      { id: 12, title: '金牌面试题', },
      { id: 12, title: '金牌面试题1', },
      { id: 12, title: '金牌面试题2', },
      { id: 12, title: '金牌面试题3', },
      { id: 12, title: '金牌面试题4', },
      { id: 12, title: '金牌面试题5', },
      { id: 12, title: '金牌面试题6', },
    ]
  }
  componentDidMount() {
    // 获取token
    let token = localStorage.getItem('x-auth-token')
    if (token) {
      FsatApi().then(res => {
        console.log("点击")
        return res
      }).then((res) => {
        console.log(res)
        this.setState({ records: res.data.records })
      })
    } else {
      // 显示loading
      this.props.showLoadingFn()
      setTimeout(() => {
        // 关闭loding
        this.props.hidenLoadingFn()
        // 跳转登录页
        this.props.history.push('/login')
      }, 2000)

    }
  }
  // 刷题
  shuaTi = () => {
    // 跳转页面
    // console.log(this.props)
    this.props.history.push('exam')
  }
  render() {
    const { records } = this.state;
    return (

      <div className="ait">
        {records.map(item => (
          <li key={item.id}>
            <img src={subject} alt="" />
            <div className='tit'>{item.title}</div>
            <div className="shua" onClick={this.shuaTi}>刷题</div>
          </li>
        ))}

      </div>
    );
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    showLoadingFn() {
      dispatch({ type: 'showLoadingFn' })
    },
    hidenLoadingFn() {
      dispatch({ type: ' hidenLoadingFn' })
    }
  }
}

export default connect(null, mapDispatchToProps)(Fast);